﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions; 
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.ViewModels;
using TimeSplicerModule.Factories;
using Microsoft.Practices.Unity;
using TimeSplicerModule.Controllers;

namespace TimeSplicerModule.Views
{
    /// <summary>
    /// Interaction logic for ApplicationRoleListView.xaml
    /// </summary>
    public partial class ApplicationRoleListView : UserControl, INavigationAware
    {
        private ApplicationRoleListViewModel Model;
        private readonly IUnityContainer _container;
        private readonly IApplicationRoleFactory _applicationRoleFactory;
        private readonly IApplicationRoleController _applicationRoleController;

        public ApplicationRoleListView(IUnityContainer container, IApplicationRoleFactory applicationRoleFactory, IApplicationRoleController applicationRoleController)
        {
            InitializeComponent();

            _container = container;
            _applicationRoleFactory = applicationRoleFactory;
            _applicationRoleController = applicationRoleController;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            Model = new ApplicationRoleListViewModel();
            Model.ApplicationRoles = _applicationRoleFactory.GetList();

            this.DataContext = Model;
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid grid = sender as DataGrid;

            if (grid.SelectedItem != null)
            {
                var role = grid.SelectedItem as ApplicationRoleModel;
                _applicationRoleController.NavigateToItemView(role.ApplicationRoleGuid);
            }
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            _applicationRoleController.NavigateToItemView();
        }
    }
}
